﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

using CashFlow.Web.Def;
using StackExchange.Redis;

namespace CashFlow.Web.Common
{
    public class RegExpTool
    {
        public static bool IsDate(string date)
        {
            if (!string.IsNullOrEmpty(date))
            {
                string pattern = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|" +
                            "((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|" +
                            "((0[48]|[2468][048]|[3579][26])00))-02-29)$";
                Regex reg = new Regex(pattern);
                return reg.IsMatch(date);
            }
            return false;
        }

        public static bool IsPhone(string phone)
        {
            if (!string.IsNullOrEmpty(phone))
            {
                string pattern = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$";
                Regex reg = new Regex(pattern);
                return reg.IsMatch(phone);
            }
            return false;
        }

        public static bool IsName(string name)
        {
            if (!string.IsNullOrEmpty(name))
            {
                string pattern = "[\\u4e00-\\u9fa5a-zA-Z0-9]{2,10}";
                Regex reg = new Regex(pattern);
                return reg.IsMatch(name);
            }
            return false;
        }

        public static bool IsInvitationCode(string code)
        {
            if (!string.IsNullOrEmpty(code))
            {
                string pattern = "[A-Z]{6}";
                Regex reg = new Regex(pattern);
                return reg.IsMatch(code);
            }
            return false;
        }

        public static bool IsUserName(string username)
        {
            if (!string.IsNullOrEmpty(username))
            {
                string pattern = "[a-zA-Z0-9]{5,20}";
                Regex reg = new Regex(pattern);
                return reg.IsMatch(username);
            }
            return false;
        }

        public static bool IsPassword(string pwd)
        {
            if (!string.IsNullOrEmpty(pwd))
            {
                string pattern = "[a-zA-Z0-9]{6,20}";
                Regex reg = new Regex(pattern);
                return reg.IsMatch(pwd);
            }
            return false;
        }

        public static bool IsVerifyCode(string code)
        {
            if (!string.IsNullOrEmpty(code))
            {
                string pattern = "[a-zA-Z0-9]{4}";
                Regex reg = new Regex(pattern);
                return reg.IsMatch(code);
            }
            return false;
        }

        public static bool IsOrderNo(string orderNo)
        {
            if (!string.IsNullOrEmpty(orderNo))
            {
                string pattern = "E[0-9]{19}";
                Regex reg = new Regex(pattern);
                return reg.IsMatch(orderNo);
            }
            return false;
        }

        public static bool IsImagePath(string path)
        {
            if (!string.IsNullOrEmpty(path))
            {
                string pattern = "[a-zA-Z0-9\\/\\-_]{10,60}[\\.jpg|\\.jpeg|\\.gif|\\.png]";
                Regex reg = new Regex(pattern);
                return reg.IsMatch(path);
            }
            return false;
        }

        public static bool IsOrderAmount(decimal amount)
        {
            if (amount >= MyConfig.OrderAmountMin
                && amount <= MyConfig.OrderAmountMax)
            {
                return true;
            }
            return false;
        }

        public static bool IsBankAccountName(string name)
        {
            if (!string.IsNullOrEmpty(name))
            {
                string pattern = "[\\u4e00-\\u9fa5]{2,5}";
                Regex reg = new Regex(pattern);
                return reg.IsMatch(name);
            }
            return false;
        }

        public static bool IsOrderState(int state)
        {
            if (state == (int)EOrderState.NotPaied
                || state == (int)EOrderState.Paied
                || state == (int)EOrderState.Recved
                || state == (int)EOrderState.Fetch)
            {
                return true;
            }
            return false;
        }

        public static bool IsShopManagerLevelPercent(decimal percent)
        {
            if (percent == Consts.ShopManagerLevelPercent_Junior
                || percent == Consts.ShopManagerLevelPercent_Intermidate
                || percent == Consts.ShopManagerLevelPercent_Senior
                || percent == Consts.ShopManagerLevelPercent_FlagShip
                || percent == Consts.ShopManagerLevelPercent_DirectSale)
            {
                return true;
            }
            return false;
        }

        public static bool IsShopNo(string shopNo)
        {
            if (!string.IsNullOrEmpty(shopNo))
            {
                string pattern = "[a-zA-Z0-9]{6,15}";
                Regex reg = new Regex(pattern);
                return reg.IsMatch(shopNo);
            }
            return false;
        }
    }
}